package com.bobo.encryption.asymmetric;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;

/**
 * Java实现非对称加密（RSA）
 * @author win7
 *
 */
public class RSAtest {

	private static String bobo="www.baobao.com";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JdkRSA();
	}
	/**
	 * jdk实现RSA加解密
	 */
	public static void JdkRSA(){
		try {
		 //1.初始化key秘钥
			KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
			keyPairGenerator.initialize(512);
			KeyPair keypair= keyPairGenerator.generateKeyPair();
			RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) keypair.getPrivate();
			RSAPublicKey rsaPublicKey=(RSAPublicKey) keypair.getPublic();
			//转换key秘钥
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
			KeyFactory keyFactory=KeyFactory.getInstance("RSA");
			PrivateKey privateKey= keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			
		//2.私钥加密,公钥解密————私钥加密
			Cipher cipher=Cipher.getInstance("RSA");
			cipher.init(Cipher.ENCRYPT_MODE, privateKey);
			byte[] result= cipher.doFinal(bobo.getBytes());
			System.out.println("私钥加密,公钥解密————私钥加密:"+HexBin.encode(result));
			
	    //3.私钥加密,公钥解密————公钥解密
			X509EncodedKeySpec x509EncodedKeySpec= new X509EncodedKeySpec(rsaPublicKey.getEncoded());
			KeyFactory publickeyfactory=KeyFactory.getInstance("RSA");
			PublicKey publickey= publickeyfactory.generatePublic(x509EncodedKeySpec);
			Cipher cipherpub=Cipher.getInstance("RSA");
			cipherpub.init(Cipher.DECRYPT_MODE, publickey);
			byte[] resultpub= cipherpub.doFinal(result);
			System.out.println("私钥加密,公钥解密————公钥解密:"+new String(resultpub));
		
			
			
		//反过来也一样（公钥加密,私钥解密）
			//公钥加密,私钥解密————公钥加密
			X509EncodedKeySpec x509EncodedKeySpecpub=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
			KeyFactory keyFactorypub=KeyFactory.getInstance("RSA");
			PublicKey publickeypub= keyFactorypub.generatePublic(x509EncodedKeySpecpub);
			//加密
			Cipher cipherpubic=Cipher.getInstance("RSA");
			cipherpubic.init(Cipher.ENCRYPT_MODE, publickeypub);
			byte[] resultpublic= cipherpubic.doFinal(bobo.getBytes());
			System.out.println("公钥加密,私钥解密————公钥加密:"+HexBin.encode(resultpublic));
			
			//公钥加密,私钥解密————私钥解密
			PKCS8EncodedKeySpec pkcs8EncodedKeySpecpri=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
			KeyFactory keyFactorypri=KeyFactory.getInstance("RSA");
			PrivateKey privatekeydecrypt= keyFactorypri.generatePrivate(pkcs8EncodedKeySpecpri);
			
			//解密
			Cipher cipherpri=Cipher.getInstance("RSA");
			cipherpri.init(Cipher.DECRYPT_MODE, privatekeydecrypt);
			byte[] resultdecrypt= cipherpri.doFinal(resultpublic);
			System.out.println("公钥加密,私钥解密————私钥解密:"+new String(resultdecrypt));
			
			
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
